package com.ciis.system.user.service.impl;

import com.ciis.base.CIISDES3;
import com.ciis.base.page.IPage;
import com.ciis.system.user.domain.TSysUser;
import com.ciis.system.user.mapper.ITSysUserMapper;
import com.ciis.system.user.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: zhangdajun
 * Date: 14-5-24
 * Time: 下午3:17
 * To change this template use File | Settings | File Templates.
 */
@Service("userService")
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    ITSysUserMapper sysUserMapper;

    @Override
    public void save(TSysUser user) throws Exception {
        if(user != null && user.getPassword() != null){
            user.setPassword(CIISDES3.encode(user.getPassword()));
        }
        sysUserMapper.save(user);
    }

    @Override
    public void update(TSysUser user) throws Exception {
        sysUserMapper.update(user);
    }

    @Override
    public void delete(String idStr) throws Exception {
        String ids[] = idStr.split(",");
        for(String id : ids){
            sysUserMapper.delete(id);
        }
    }

    @Override
    public TSysUser getUserById(String id) throws Exception {
        return sysUserMapper.getUserById(id);
    }

    @Override
    public TSysUser getUserByName(String name) throws Exception {
        return sysUserMapper.getUserByName(name);
    }

    @Override
    public IPage<TSysUser> getUserListByOrgId(IPage<TSysUser> page, String orgId) throws Exception {
        return sysUserMapper.getUserListByOrgId(page,orgId);
    }

    public TSysUser validate(String name,String password){
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("loginId",name);
        params.put("password",password);
        return sysUserMapper.validateUser(params);
    }
}
